package com.jmb.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmb.api.common.exception.LeaseException;
import com.jmb.api.common.result.ResultCodeEnum;
import com.jmb.api.model.entity.DataFactory;
import com.jmb.api.model.entity.Db;
import com.jmb.api.model.to.Factory.DataFactoryTO;
import com.jmb.api.model.vo.Factory.DataFactoryVO;
import com.jmb.api.model.vo.api.suite.ApiCaseSuiteVO;
import com.jmb.api.service.ApiCaseSuiteCaseRefService;
import com.jmb.api.service.ApiCaseSuiteService;
import com.jmb.api.service.DataFactoryService;
import com.jmb.api.mapper.DataFactoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
* @author jinbo
* @description 针对表【data_factory】的数据库操作Service实现
* @createDate 2025-04-08 13:53:56
*/
@Service
public class DataFactoryServiceImpl extends ServiceImpl<DataFactoryMapper, DataFactory>
    implements DataFactoryService{

    @Autowired
    private DataFactoryMapper dataFactoryMapper;
    @Autowired
    private DbServiceImpl dbService;
    @Autowired
    private ApiCaseSuiteService apiCaseSuiteService;
    @Autowired
    private ApiCaseSuiteCaseRefService caseSuiteCaseRefService;

    /**
     * 保存数据工厂
     * @param dataFactoryDO 入参对象
     */
    @Override
    public void saveDataFactory(DataFactory dataFactoryDO) {
        // 数据校验
        checkDO(dataFactoryDO);
        DataFactory factoryDO = this.dealWithDO(dataFactoryDO);
        this.save(factoryDO);

    }

    /**
     * 修改数据工厂
     * @param dataFactoryDO 入参对象
     */
    @Override
    public void modifyDataFactory(DataFactory dataFactoryDO) {
        // 数据校验
        checkDO(dataFactoryDO);
        DataFactory factoryDO = this.dealWithDO(dataFactoryDO);
        LambdaUpdateWrapper<DataFactory> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(DataFactory::getId,dataFactoryDO.getId());
        this.update(factoryDO,updateWrapper);

    }

    /**
     * 删除数据工厂
     * @param id 主键id
     */
    @Override
    public void removeDataFactoryById(Integer id) {
        if (id == null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        this.removeById(id);
    }

    @Override
    public DataFactoryVO findDataFactoryById(Integer id) {
        String  name = "";
        if (id == null){
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        DataFactoryVO dataFactoryVO  = dataFactoryMapper.selectDataFactoryById(id);
        if (dataFactoryVO.getType() == 0){
            // SQL
            Integer sqlDbId = dataFactoryVO.getSqlDbId();
            Db db = dbService.findDbById(sqlDbId);
            name = db.getName();
        }else if (dataFactoryVO.getType() == 1){
            // 接口
            Integer interfaceSuiteId = dataFactoryVO.getInterfaceSuiteId();
            ApiCaseSuiteVO suite = apiCaseSuiteService.findApiCaseSuiteById(interfaceSuiteId);
            name = suite.getSuiteName();
        } else if (dataFactoryVO.getType() == 2){
            // UI
            System.out.println("UI需完善");
        }
        dataFactoryVO.setElementName(name);

        return dataFactoryVO;
    }

    @Override
    public IPage<DataFactoryVO> findDataFactoryList(IPage<DataFactoryVO> page, DataFactoryTO dataFactoryDTO) {
        return dataFactoryMapper.selectDataFactoryList(page,dataFactoryDTO);
    }

    @Override
    public long executeDataFactory(Integer id, String executor) {
        if (id == null) {
            throw new LeaseException(ResultCodeEnum.WEB_API_ID_NOT_NULL);
        }
        DataFactoryVO factoryVO = this.findDataFactoryById(id);
        if (factoryVO.getType() == 0) {
            // sql
            // 获取运行环境
            Db db = dbService.findDbById(factoryVO.getSqlDbId());

        } else if (factoryVO.getType() == 1) {
            // 接口
            Integer suiteId = factoryVO.getInterfaceSuiteId();
            if (factoryVO.getExecuteType() == 1){
                // 串行
                for (int i = 0;  i<factoryVO.getTimes(); i++) {
                    caseSuiteCaseRefService.executeSuiteCaseById(suiteId,executor);
                }
                // 获取开始时间
                long begin = System.currentTimeMillis();
                // 获取结束时间
                long end = System.currentTimeMillis();
                // 返回总耗时间
               return end-begin;
            }else {
                // 并行
                ArrayList<Integer> list = new ArrayList<>();
            }

        } else if (factoryVO.getType() == 2){
            // UI
            System.out.println("UI需完善");
        }
        return 0;
    }
    /**
     * 检查数据工厂
     * @param dataFactory DO
     */
    private void checkDO(DataFactory dataFactory){
        if(dataFactory.getType()==0){
            //sql
            if (dataFactory.getSqlDbId()==null){
                throw new RuntimeException("数据工厂类型为sql时，数据库不能为空");
            } else if (dataFactory.getSqlRunDev()==null){
                throw new RuntimeException("数据工厂类型为sql时，运行环境不能为空");
            } else if (dataFactory.getSqlStr()==null){
                throw new RuntimeException("数据工厂类型为sql时，sql语句不能为空");
            }else if (dataFactory.getSqlStr().isEmpty()){
                throw new RuntimeException("数据工厂类型为sql时，sql语句不能为空");
            }
        }else if(dataFactory.getType()==1){
            //接口
            if (dataFactory.getInterfaceSuiteId()==null){
                throw new RuntimeException("数据工厂类型为接口时，接口集合不能为空");
            }
        }else if(dataFactory.getType()==2){
            //UI
            if (dataFactory.getUiSuiteId()==null){
                throw new RuntimeException("数据工厂类型为UI时，UI集合不能为空");
            }
        }
    }
    /**
     * 数据处理
     * @param dataFactoryDO dataFactoryDO
     * @return dataFactoryDO
     */
    private DataFactory dealWithDO(DataFactory dataFactoryDO) {
        Integer type = dataFactoryDO.getType();
        if (type == 0) { // sql
            dataFactoryDO.setInterfaceSuiteId(null);
            dataFactoryDO.setUiSuiteId(null);
        } else if (type == 1) { // 接口
            dataFactoryDO.setSqlDbId(null);
            dataFactoryDO.setSqlRunDev(null);
            dataFactoryDO.setSqlStr(null);
            dataFactoryDO.setUiSuiteId(null);
        } else if (type == 2) { // ui
            dataFactoryDO.setSqlDbId(null);
            dataFactoryDO.setSqlRunDev(null);
            dataFactoryDO.setSqlStr(null);
            dataFactoryDO.setInterfaceSuiteId(null);
        }
        return dataFactoryDO;
    }
}




